package cn.itcast.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: zhaojie
 * @Date: Created in 10:50 2018/8/13
 * @Description: 权限控制器
 */
public class AccessTokenFilter extends ZuulFilter {



    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String token = request.getParameter("token");
        //暂时简单化测试
        if (null != token ) {
            // 对该请求进行路由
            ctx.setSendZuulResponse(true);
            ctx.setResponseStatusCode(200);
            // 设值，可以在多个过滤器时使用
            ctx.set("isSuccess", true);
            return null;
        } else {
           // 过滤该请求，不对其进行路由
            ctx.setSendZuulResponse(false);
           // 返回错误码
            ctx.setResponseStatusCode(403);
            // 返回错误内容
            ctx.setResponseBody("{\"result\":\"Request illegal!the token is null\"}");
            ctx.set("isSuccess", false);
            return null;
        }
    }
}
